var WxNotificationCenter = require('../../utils/WxNotificationCenter.js');
var comment = require("../../utils/utils.js")
var app = getApp();

var that;

Page({
	data: {
		visual: 'hidden'
	},
	onLoad: function (options) {
		that = this;
		if (options.isSwitchAddress) {
			that.setData({
				isSwitchAddress: true
			});
		}
	},
	onShow: function () {
		that.getAddress();	
	},
	add: function () {
		wx.navigateTo({
			url: '../add/add'
		});
	},
	getAddress: function () {
    //查询用户地址
    wx.request({
      url: comment.baseUrl + "/wx/address/queryAddressByUserId",
      data: {
        userId: app.globalData.openId
      },
      success: function (results) {
        results = results.data;
        that.setData({
          addressList: results,
          visual: results.length ? 'hidden' : 'show'
        });
      }
    });
	},
	edit: function (e) {
		var index = e.currentTarget.dataset.index;
    var objectId = that.data.addressList[index].addressId;
		wx.navigateTo({
			url: '../add/add?objectId=' + objectId
		})
	},
	selectAddress: function (e) {
		if (!that.data.isSwitchAddress) {
			return;
		}
		var index = e.currentTarget.dataset.index;
		//设置addressId通知给上个页面，并返回
		WxNotificationCenter.postNotificationName("addressSelectedNotification", that.data.addressList[index].addressId);
		wx.navigateBack();
	}
})